<?php
/**
 * Created by PhpStorm.
 * User: Digvijay
 * Date: 10/19/14
 * Time: 10:52 AM
 */

class Content extends MY_Controller {
    public function termsandconditions() {
        $data = array("pageTitle" => "Terms & Conditions");
        $this->load->view("admin/termsandconditions", $data);
    }

    public function about() {
        $data = array("pageTitle" => "About");
        $this->load->view("admin/about", $data);
    }

    public function specialOffers() {
        $offers = $this->findAll("Specialoffer");
        $systemConfig = $this->getSystemConfig(Systemconfig::SPECIAL_OFFER);
        $data = array("pageTitle" => "Special Offers", "offers"=>$offers,"systemConfig"=>$systemConfig);
        $this->load->view("admin/special-offers", $data);
    }

    public function saveContent($type = null) {
        $response = new Response();
        try {
            if ($type == 'TERMS') {
                $type = Contents::CONTENT_TERMS_AND_CONDITION;
            } else if ($type == 'ABOUT') {
                $type = Contents::ABOUT;
            } else {
                throw new RuntimeException("Invalid Request");
            }
            $id = strip_tags(trim($this->input->post("content-id")));
            $description = trim($this->input->post("content-description"));
            $content = new Contents();
            if ($id != null || $id != "") {
                $content = $this->findById("Contents", $id);
            }
            $content->setDescription($description);
            $content->setType($type);
            $this->save($content);
            $response->setData($content);
        } catch (Exception $e) {
            $response->setError(true);
            $response->setErrorMessage($e->getMessage());
            log_message("error", $e->getMessage());
        }
        header('Content-Type: application/json');
        echo json_encode($response, JSON_PRETTY_PRINT);
    }

    public function getContent($type = null) {
        $response = new Response();
        try {
            if ($type == 'TERMS') {
                $type = Contents::CONTENT_TERMS_AND_CONDITION;
            } else if ($type == 'ABOUT') {
                $type = Contents::ABOUT;
            } else {
                throw new RuntimeException("Invalid Request");
            }
            $content = $this->findContentByType($type);
            $response->setData($content);
        } catch (Exception $e) {
            $response->setError(true);
            $response->setErrorMessage($e->getMessage());
            log_message("error", $e->getMessage());
        }
        header('Content-Type: application/json');
        echo json_encode($response, JSON_PRETTY_PRINT);
    }

    public function getSpecialOffers($id = null){
        $response = new Response();
        try {
            if ($id == null && $id == "") {
                throw new RuntimeException("Invalid Request");
            }
            $content = $this->findById("Specialoffer",$id);
            $response->setData($content);
        } catch (Exception $e) {
            $response->setError(true);
            $response->setErrorMessage($e->getMessage());
            log_message("error", $e->getMessage());
        }
        header('Content-Type: application/json');
        echo json_encode($response, JSON_PRETTY_PRINT);
    }

    public function saveSpecialOffer() {
        $response = new Response();
        try {
            $id = strip_tags(trim($this->input->post("special-offer-id")));
            $imageName = trim($this->input->post("special-image-name"));
            $description = trim($this->input->post("special-details"));

            if ($id == null || $id == "") {
                throw new RuntimeException("Invalid Request");
            }
            $specialOffer = $this->findById("Specialoffer", $id);
            if ($specialOffer == null) {
                throw new RuntimeException("Please update DB, Contact Support team.");
            }
            $specialOffer->setDescription($description);
            $specialOffer->setImage($imageName);
            $this->save($specialOffer);
        } catch (Exception $e) {
            $response->setError(true);
            $response->setErrorMessage($e->getMessage());
        }
        header('Content-Type: application/json');
        echo json_encode($response, JSON_PRETTY_PRINT);
    }

     function enableDisableSpecialOffers($opt = null){
         $response = new Response();
         try {
             if($opt == null || $opt == ""){
                 throw new RuntimeException("Invalid Request");
             }
             $systemConfig = $this->getSystemConfig(Systemconfig::SPECIAL_OFFER);
             $option = "false";
             if($opt == "true"){
                 $option = "true";
             }
             $systemConfig->setValue($option);
             $this->save($systemConfig);
             $response->setData($systemConfig);
         } catch (Exception $e) {
             $response->setError(true);
             $response->setErrorMessage($e->getMessage());
         }
         header('Content-Type: application/json');
         echo json_encode($response, JSON_PRETTY_PRINT);
    }
} 